package com.ruoyi.project.manhua.chapter.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.project.manhua.mistake.service.IMhClimbMistakeService;
import com.ruoyi.project.manhua.newXml.MhChapterNewMapper;
import com.ruoyi.project.manhua.newXml.MhPageNewMapper;
import com.ruoyi.project.manhua.page.domain.MhPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.manhua.chapter.domain.MhChapter;
import com.ruoyi.project.manhua.chapter.service.IMhChapterService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 漫画章节 Service业务层处理
 *
 * @author yuan
 * @date 2021-01-13
 */
@Service
public class MhChapterServiceImpl implements IMhChapterService {
    @Autowired
    private MhChapterNewMapper mhChapterNewMapper;

    @Autowired
    private MhPageNewMapper mhPageNewMapper;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    @Autowired
    private IMhClimbMistakeService iMhClimbMistakeService;

    /**
     * 查询漫画章节
     *
     * @param id 漫画章节 ID
     * @return 漫画章节
     */
    @Override
    public MhChapter selectMhChapterById(Long id) {
        return mhChapterNewMapper.selectMhChapterById(id);
    }

    /**
     * 查询漫画章节 列表
     *
     * @param mhChapter 漫画章节
     * @return 漫画章节
     */
    @Override
    public List<MhChapter> selectMhChapterList(MhChapter mhChapter) {
        return mhChapterNewMapper.selectMhChapterList(mhChapter);
    }

    /**
     * 新增漫画章节
     *
     * @param mhChapter 漫画章节
     * @return 结果
     */
    @Override
    public int insertMhChapter(MhChapter mhChapter) {
        mhChapter.setCreateTime(DateUtils.getNowDate());
        return mhChapterNewMapper.insertMhChapter(mhChapter);
    }

    /**
     * 修改漫画章节
     *
     * @param mhChapter 漫画章节
     * @return 结果
     */
    @Override
    public int updateMhChapter(MhChapter mhChapter) {
        mhChapter.setUpdateTime(DateUtils.getNowDate());
        return mhChapterNewMapper.updateMhChapter(mhChapter);
    }

    /**
     * 删除漫画章节 对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMhChapterByIds(String ids) {
        String[] array = Convert.toStrArray(ids);

        Arrays.stream(array).forEach((e) -> {

            deleteMhChapterById(Long.valueOf(e));

        });

        return 1;
        //return mhChapterNewMapper.deleteMhChapterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除漫画章节 信息
     *
     * @param id 漫画章节 ID
     * @return 结果
     */
    @Override
    public int deleteMhChapterById(Long id) {

        List<MhPage> mhPages = mhPageNewMapper.selectMhPageList(new MhPage(null, null, id, null, null));

        mhPages.forEach((r) -> {

            if (r.getSrc() != null && !"".equals(r.getSrc()) && r.getSrc().indexOf("http") == -1) {

                Integer delete = qiNiuYunUtils.getDelete(r.getSrc());

                if (delete != 200) {
                    throw new RuntimeException("删除七牛云图片失败!");
                }

            }
            mhPageNewMapper.deleteMhPageById(r.getId());
            /*删除 篇章*/
            iMhClimbMistakeService.deleteMhClimbMistakeByPageId(r.getId());
        });

        return mhChapterNewMapper.deleteMhChapterById(id);
    }
}
